const { conn } = require('../gather/mongo');
const { getPageNum } = require('../utils/getpageNum.js');
// 获取商品列表
exports.getShopList = (req, res) => {
    let { pageNum, pageSize } = req.query;
    if (!pageNum || !pageSize) {
        res.json({
            code: 400,
            msg: '参数错误'
        })
    }
    let page = getPageNum(pageNum, pageSize);
    // 查找商品总数
    const sq1 = `SELECT COUNT(*) FROM shop_list`
    conn.query(sq1, (err, result) => {
        if (err) {
            console.log(err);
            res.json({
                code: 500,
                msg: '服务器错误'
            })
        } else {
            getpageShop(result)
        }
    })
    // 根据页面返回数据
    function getpageShop(result) {
        const sql1 = `SELECT * FROM shop_list LIMIT ${page},${pageSize} `;
        conn.query(sql1, (err, result1) => {
            if (err) {
                res.json({
                    code: 500,
                    msg: '服务器错误'
                })
            } else {
                res.json({
                    code: 200,
                    msg: '查询成功',
                    data: result1,
                    total: result[0]['COUNT(*)'],
                })
            }
        })
    }

}